//
//  BarcodeInputCell.swift
//  Wmsmobile
//
//  Created by Nick xu on 16/8/5.
//  Copyright © 2016年 hddden. All rights reserved.
//

import UIKit
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l > r
  default:
    return rhs < lhs
  }
}


protocol HDKeyboardInputCellDelegate:NSObjectProtocol {
    func clicKKeyboardReturn(_ cell: HDKeyboardInputCell)
}

class HDKeyboardInputCell: HDFounctionBaseCell , UITextFieldDelegate , AVQRCodeViewManagerInputDelegate , HDInputViewStateProtocol, HDgetAndSetProtocol{
    //MARK: - property
    var precision: Int?  //控制输入框内数字的精确度
    weak var InputDelegate:HDKeyboardInputCellDelegate?{
        didSet{
            print(InputDelegate!)
        }
    }
    lazy var codeInputView: WmInputView = {
        let view = WmInputView(frame: CGRect.zero, cameraAble: false)
        view.textField.delegate = self
        return view
    }()
    var inputState:inputViewState = inputViewState.disabled{
        didSet{
            switch inputState { //改变状态
            case .activated:
                codeInputView.isUserInteractionEnabled = true
//                codeInputView.textField.inputView = customTextfield.emptyView()
                codeInputView.textField.resignFirstResponder()
                if codeInputView.textField.canBecomeFirstResponder{
                    codeInputView.textField.becomeFirstResponder()
                }
                AVQRCodeViewManager.shareAVQRCodeViewManager.InputDelegate = self
                self.updateButtonState()
            case .disabled:
                codeInputView.isUserInteractionEnabled = false
                codeInputView.button_clear.isHidden = true
            case .wrong:
                self.updateButtonState()
                codeInputView.layer.borderColor = UIColor.red.cgColor
                codeInputView.isUserInteractionEnabled = true
                codeInputView.textField.text = nil
                codeInputView.button_clear.isHidden = true
                codeInputView.textField.becomeFirstResponder()
            }
    }
    }
    var state:inputViewState?{
        get{
            return inputState
        }
        set{
            inputState = newValue!
        }
        
        
    }

    //MARK: - life cycle
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: "BarcodeInputCell")
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.setUpUI()
    }
    
    required init() {
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: "BarcodeInputCell")
        self.setUpUI()
    }
    //MARK: - delegate
    func getText() -> (String?) {
        return self.codeInputView.textField.text
    }
    func SetText(_ text: String?) {
        self.codeInputView.textField.text = text
    }
    func finishScan(_ msg: String?) {
        if self.InputDelegate != nil {
            self.InputDelegate?.clicKKeyboardReturn(self)
        }
    }
    //MARK: - public
    override func clearContent() {
        self.codeInputView.textField.text = ""
    }
    //MARK: - textFieldDelegate
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        var futureString = textField.text ?? ""
        futureString.insert(contentsOf: string.characters, at: futureString.characters.index(futureString.startIndex, offsetBy: range.location))
        return numFilter(2, string: futureString)
        }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if self.InputDelegate != nil {
            self.InputDelegate?.clicKKeyboardReturn(self)
        }
        return true
    }
    //MARK: - private
    func updateButtonState(){
        codeInputView.button_clear.isHidden = codeInputView.textField.text?.characters.count > 0 ? false : true
    }
    func setUpUI(){
        self.codeInputView.textField.addTarget(self, action: #selector(HDKeyboardInputCell.updateButtonState), for: UIControlEvents.editingChanged)
        self.state = inputViewState.disabled
        self.codeInputView.button_camera.isHidden = true
        self.addSubview(self.codeInputView)
        codeInputView.snp.makeConstraints { (make) in
            make.leading.equalTo(self).offset(inputViewToCellSpace)
            make.top.equalTo(self).offset(FTCellTopSpace+4)
            make.bottom.equalTo(self).offset(-FTCellTopSpace-5)
            make.trailing.equalTo(self).offset(-9)
        }
    }
    
    
}
